import { storage, createStorage } from '@/utils/storage'
import api from '@/api'
import projectSetting from '@/settings/projectSetting.js'
import designSetting from '@/settings/designSetting.js'
const { globTitle, globLocalHost } = projectSetting
const { appTheme, themeList } = designSetting
const itemSessionStorage = createStorage({ storage: sessionStorage })
import { GLOB_CONFIG_INFO, CURRENT_SHARE_CODE, GLOB_MHM_ID } from '@/store/mutation-types'
export default {
  namespaced: true,
  state: {
    projectconfigData: storage.get(GLOB_CONFIG_INFO, null), // 项目配置信息
    globalSwitch: null, // 全局开关
    globCartNumber: 0, // 购物车数量
    globThirdLoginConfig: null, // 第三方配置
    globShareCode: itemSessionStorage.get(CURRENT_SHARE_CODE, ''), // 分享
    menus: [], // 头部导航
    e_app_mhm_id: storage.get(GLOB_MHM_ID, 1), // 机构id
    globNotifyUnread: 0 // 未读消息
  },
  getters: {
    // 标题
    getGlobTitle(state) {
      if (state.projectconfigData) {
        return state.projectconfigData.site_info.title || globTitle
      }
      return globTitle
    },
    // 主题key值
    getGlobTheme(state) {
      if (state.projectconfigData) {
        let theme = state.projectconfigData.theme_color || appTheme
        return theme
      }
      return appTheme
    },
    // 主题颜色值
    getGlobThemeColor(state) {
      if (state.projectconfigData) {
        let theme = state.projectconfigData.theme_color || appTheme
        return themeList[theme]
      }
      return themeList[appTheme]
    },
    // 登录配置
    getGlobLoginConfig(state) {
      if (state.projectconfigData) {
        return state.projectconfigData.login_config
      }
      return {}
    },
    // 登录配置/登录name
    getGlobLoginRouteName(state) {
      const project = state.projectconfigData
      if (project) {
        const config = state.projectconfigData.login_config
        if (config.verify == 1 && config.user == 1) {
          return 'login_account'
        } else if (config.verify == 1) {
          return 'login_verify'
        } else if (config.user == 1) {
          return 'login_account'
        }
      }
      return ''
    },
    // 登录配置/登录按钮链接
    getGlobLoginConfigLink(state) {
      const project = state.projectconfigData
      if (project) {
        const config = state.projectconfigData.login_config
        if (config.verify == 1 && config.user == 1) {
          return '/login/account'
        } else if (config.verify == 1) {
          return '/login/verify'
        } else if (config.user == 1) {
          return '/login/account'
        }
      }
      return ''
    },
    // 站点信息
    getGlobSiteInfo(state) {
      if (state.projectconfigData) {
        return state.projectconfigData.site_info || {}
      }
      return {}
    },
    // 笔记开关
    getGlobCourseNoteSwitch(state) {
      if (state.globalSwitch) {
        return state.globalSwitch.note
      }
      return 0
    },
    // 评论开关
    getGlobCourseCommentSwitch(state) {
      if (state.globalSwitch) {
        return state.globalSwitch.comment
      }
      return 0
    },
    // 课程课件
    getGlobCoursewareSwitch(state) {
      if (state.globalSwitch) {
        return state.globalSwitch.courseware
      }
      return 0
    },
    // 课程考试
    getGlobCourseExamSwitch(state) {
      if (state.globalSwitch) {
        return state.globalSwitch.exam
      }
      return 0
    },
    // 分类开关
    getGlobCategoryPositionSwitch(state) {
      if (state.globalSwitch) {
        return state.globalSwitch.category_position
      }
      return 3
    },
    // 人脸状态
    getGlobFaceStatus(state, getters, rootState) {
      // state, getters, rootState, rootGetters // 第三和第四个参数带命名空间的模块内访问全局内容
      if (rootState.user) {
        return rootState.user.info.face_verified
      }
      return 0
    },
    // 进度条
    getGlobProgress(state) {
      if (state.globalSwitch) {
        return state.globalSwitch.noSpeedUp || 0
      }
      return 0
    },
    // 主菜单
    getMenus(state) {
      return state.menus
    },
    // 协议标题
    getAgreementTitle: state => key => {
      const list = state.projectconfigData.content_conf
      if (list) {
        const data = list.find(item => item.key === key)
        return data ? data.title : ''
      }
      return ''
    }
  },
  mutations: {
    // 配置信息
    setProjectconfig(state, data) {
      state.projectconfigData = data
      storage.set(GLOB_CONFIG_INFO, data)
    },
    // 开关
    setSwitch(state, data) {
      state.globalSwitch = data
    },
    // 购物车数量
    setCartNumber(state, data) {
      state.globCartNumber = data
    },
    // 未读消息
    setNotifyUnread(state, value) {
      state.globNotifyUnread = value
    },
    // 第三方登录
    setThirdLoginConfig(state, data) {
      state.globThirdLoginConfig = data
    },
    // 分享code
    setGlobShareCode(state, code) {
      state.globShareCode = code
      itemSessionStorage.set(CURRENT_SHARE_CODE, code)
    },
    // 设置菜单
    setMenus(state, menus) {
      state.menus = menus
    },
    // 机构id
    setGlobSchoolId(state, id) {
      state.e_app_mhm_id = id
      storage.set(GLOB_MHM_ID, id)
    }
  },
  actions: {
    // 初始化
    setConfig({ commit }) {
      api.configInit(res => {
        const { data } = res

        // 统计
        if (data.site_info.stats_code) {
          let stats_code = document.createElement('script')
          stats_code.src = data.site_info.stats_code
          document.body.appendChild(stats_code)
        }

        // 主题
        window.document.documentElement.setAttribute('data-theme', data.theme_color)
        commit('setProjectconfig', data)
      })
    },
    // 开关
    setGlobSwitch({ commit }) {
      api.getConfigSwitch().then(res => {
        const { data } = res
        commit('setSwitch', data)
      })
    },
    // 购物车数量
    setGlobCartNumber({ commit }) {
      api.getCartNum().then(res => {
        const { data } = res
        commit('setCartNumber', data)
      })
    },
    // 未读消息
    setGlobNotifyUnread({ commit }) {
      api.getUserNotifyUnread(res => {
        const { data } = res
        commit('setNotifyUnread', data)
      })
    },
    // 第三方登录配置
    setGlobThirdLoginConfig({ commit }) {
      api.getConfigThirdlogin(
        {
          redirect_uri: globLocalHost + '/thirdlogin'
        },
        res => {
          const { data } = res
          commit('setThirdLoginConfig', data)
        }
      )
    },
    // 头部菜单
    setMenus({ commit }) {
      // 设置动态路由
      api.getNaviTopList(res => {
        commit('setMenus', res.data)
      })
    }
  }
}
